﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ServiceModel;
using System.Text;

namespace SydEnergiDataService
{
    public class DataRepositoryServiceStub : IDataRepositoryService
    {
        private bool flag = false;
  
        public IEnumerable<CabinetDto> GetCabinets()
        {
            return GetCabinetsFromStream();
        }

        private IEnumerable<CabinetDto> GetCabinetsFromStream()
        {
            using(var stream = File.Open("data.bin", FileMode.Open))
            {
                var bf = new BinaryFormatter();
                return (List<CabinetDto>) bf.Deserialize(stream);
            }
        }

        public void SetCabinets(IEnumerable<CabinetDto> cabinets)
        {
            using(var stream = File.Open("data.bin", FileMode.Create))
            {
                var bf = new BinaryFormatter();
                bf.Serialize(stream, cabinets.ToList());
            }
        }

        public void InitData()
        {
            //using(Stream stream = File.Open("data.bin", FileMode.Create))
            //{
            //    BinaryFormatter bf = new BinaryFormatter();

            //    var cabinetDtos = new List<CabinetDto>();

            //    var historyEntry = new HistoryEntryDto();
            //    historyEntry.Date = DateTime.Now;
            //    historyEntry.User = "MMR";
            //    historyEntry.Reason = "Oprettet";

            //    var entry1 = new EntryDto();
            //    entry1.CabinetId = "SK107 HAC";
            //    entry1.EntryAddressDto = new AddressDto("Bygade", 22, 2400, "Kbh NV");
            //    entry1.Fiber = "Green";
            //    entry1.Tube = "Blue";
            //    entry1.EntryHistory = new List<HistoryEntryDto> { historyEntry };

            //    var entry2 = new EntryDto();
            //    entry2.CabinetId = "AK123 ADC";
            //    entry2.EntryAddressDto = new AddressDto("Husumvej", 23, 2400, "Kbh NV");
            //    entry2.Fiber = "Purple";
            //    entry2.Tube = "Orange";
            //    entry2.EntryHistory = new List<HistoryEntryDto> { historyEntry };

            //    var entry3 = new EntryDto();
            //    entry3.CabinetId = "AH345 HUA";
            //    entry3.EntryAddressDto = new AddressDto("Veksøvej", 51, 2400, "Kbh NV");
            //    entry3.Fiber = "Blue";
            //    entry3.Tube = "Orange";
            //    entry3.EntryHistory = new List<HistoryEntryDto> { historyEntry };

            //    cabinetDtos.Add(new CabinetDto("SK107 HAC", new AddressDto("Bygmestervej", 53, 2400, "Kbh NV"), new List<EntryDto> { entry1 }, 55.704374, 12.536666));
            //    cabinetDtos.Add(new CabinetDto("AK123 ADC", new AddressDto("Bygmestervej", 55, 2400, "Kbh NV"), new List<EntryDto> { entry2 }, 55.754374, 12.536666));
            //    cabinetDtos.Add(new CabinetDto("AH345 HUA", new AddressDto("Bygmestervej", 57, 2400, "Kbh NV"), new List<EntryDto> { entry3 }, 55.734374, 12.536666));

            //    bf.Serialize(stream, cabinetDtos);
            //}


        }


    }
}
